import dearpygui.dearpygui as dpg
from src.schedule_docx import Duty, Schedule

class ScheduleTabs:
    def __init__(self, schedule: Schedule, tag=0):
        self.schedule = schedule
        self.stat_rows = ["休"]
        self.stat_cols = ["值1", "值2", "值休", "正73"]
        with dpg.theme(tag="red_text_theme"):
            with dpg.theme_component(dpg.mvAll):
                dpg.add_theme_color(dpg.mvThemeCol_Text, (255, 0, 0))
        with dpg.theme(tag="tran_btn_theme"):
            with dpg.theme_component(dpg.mvAll):
                dpg.add_theme_color(dpg.mvThemeCol_Button, (0, 0, 0, 0))
                dpg.add_theme_color(dpg.mvThemeCol_ButtonHovered, (0, 0, 0, 0))
                dpg.add_theme_color(dpg.mvThemeCol_ButtonActive, (0, 0, 0, 0))
        with dpg.tab_bar(reorderable=True, tag=tag) as tag:
            self.tag = tag
        self.update()
        dpg.add_text()
        with dpg.popup(dpg.last_item(), tag=f'duty_popup', min_size=(0, 0)):
            dpg.add_checkbox(tag=f'duty_lock', label='锁定', callback=self.lock_duty)
            with dpg.group(tag=f'dutys'):
                for key in self.schedule.dutys:
                    dutys = self.schedule.dutys[key]
                    dpg.add_separator()
                    with dpg.group(horizontal=True):
                        for duty in dutys:
                            dpg.add_button(tag=f'duty_{duty}', label=duty, callback=self.set_duty)
        # dpg.show_item_registry()

    def update(self):
        dpg.delete_item(self.tag, children_only=True)
        for id in self.schedule.tables:
            with dpg.tab(tag=f'tab{id}', label=id, parent=self.tag):
                dpg.add_table(
                tag=f'table{id}',
                freeze_rows=2,
                freeze_columns=1,
                # row_background=True,
                borders_outerH=True,
                borders_outerV=True,
                borders_innerH=True,
                borders_innerV=True,
                scrollY=True,
                scrollX=True,
                policy=dpg.mvTable_SizingFixedFit,
            )
            self.update_table(id)

    def update_table(self, id):
        table_name = f'table{id}'
        dpg.delete_item(table_name, children_only=True)
        days = self.schedule.days
        weekdays = self.schedule.weekdays
        table = self.schedule.tables[id]
        row_stats = self.schedule.row_stats[id]
        col_stats = self.schedule.col_stats[id]
        # 日期
        dpg.add_table_column(parent=table_name, width_fixed=True, init_width_or_weight=50)
        for day in days:
            dpg.add_table_column(parent=table_name, label=day, width_fixed=True, init_width_or_weight=40)
        # 行统计
        for key in self.stat_rows:
            dpg.add_table_column(parent=table_name, label=key, width_fixed=True, init_width_or_weight=40)
        # 星期
        with dpg.table_row(parent=table_name):
            dpg.add_text()
            # with dpg.popup(dpg.last_item(), tag=f'{id}_duty_popup', min_size=(0, 0)):
            #     dpg.add_checkbox(tag=f'{id}_duty_lock', label='锁定', callback=self.lock_duty)
            #     with dpg.group(tag=f'{id}_dutys'):
            #         for key in self.schedule.dutys:
            #             dutys = self.schedule.dutys[key]
            #             dpg.add_separator()
            #             with dpg.group(horizontal=True):
            #                 for duty in dutys:
            #                     dpg.add_button(tag=f'{id}_duty_{duty}', label=duty, callback=self.set_duty)
            for day, weekday in zip(days, weekdays):
                dpg.add_button(tag=f'{id}_week_{day}', label=weekday, user_data=day, width=-1, callback=self.toggle_holiday)
                self.update_holiday_theme(id, day)
        # 班次
        for name in table:
            row = table[name]
            stats = row_stats[name]
            with dpg.table_row(parent=table_name):
                dpg.add_text(name)
                if len(name) > 3:
                    with dpg.tooltip(dpg.last_item()):
                        dpg.add_text(name)
                for i, cell in enumerate(row):
                    day = i + 1
                    dpg.add_button(tag=f'{id}_{name}_{day}', width=-1,
                        user_data=(id, name, day, cell), callback=self.click_cell)
                    with dpg.tooltip(dpg.last_item(), tag=f'{id}_{name}_{day}_tip'):
                        dpg.add_text(tag=f'{id}_{name}_{day}_tip_text')
                    self.update_cell(id, name, day, cell)
                for key in self.stat_rows:
                    if key in stats:
                        dpg.add_text(str(stats[key]))
                    else:
                        dpg.add_text('0')
        # 列统计
        for key in self.stat_cols:
            with dpg.table_row(parent=table_name):
                dpg.add_text(key)
                for day in self.schedule.days:
                    stats = col_stats[day]
                    if key in stats:
                        dpg.add_text(str(stats[key]))
                    else:
                        dpg.add_text('0')
            

    def click_cell(self, sender, app_data, user_data):
        id, name, day, cell = user_data
        dpg.set_value(f'duty_lock', cell.lock)
        dpg.set_item_user_data(f'duty_lock', user_data=user_data)
        if cell.lock:
            dpg.hide_item(f'dutys')
        else:
            dpg.show_item(f'dutys')
            for key in self.schedule.dutys:
                for duty in self.schedule.dutys[key]:
                    dpg.show_item(f'duty_{duty}')
                    dpg.set_item_user_data(f'duty_{duty}', user_data=user_data)
        dpg.show_item(f'duty_popup')

    def update_cell(self, id, name, day, cell):
        item = f'{id}_{name}_{day}'
        dpg.set_item_label(item, cell.value)
        dpg.set_item_user_data(item, (id, name, day, cell))
        dpg.bind_item_theme(item, 'tran_btn_theme' if cell.lock else 0)
        dpg.set_value(f'{item}_tip_text', cell.value)
        dpg.configure_item(f'{item}_tip', show = len(cell.value) > 2)

    def update_stat(self, id, name, day):
        pass

    def update_holiday_theme(self, id, day):
        if day in self.schedule.holidays:
            theme = 'red_text_theme'
        else:
            theme = 0
        dpg.bind_item_theme(f'{id}_week_{day}', theme)

    def toggle_holiday(self, sender, app_data, user_data):
        self.schedule.toggle_holiday(user_data)
        for id in self.schedule.tables:
            self.update_holiday_theme(id, user_data)

    def set_duty(self, sender, app_data, user_data):
        value = dpg.get_item_label(sender)
        id, name, day, cell = user_data
        cell.value = value
        dpg.hide_item(f'duty_popup')
        self.update_cell(id, name, day, cell)
        self.schedule.stat_row(id, name)
        self.schedule.stat_col(id, day)

    def lock_duty(self, sender, app_data, user_data):
        id, name, day, cell = user_data
        cell.lock = app_data
        dpg.hide_item(f'duty_popup')
        self.update_cell(id, name, day, cell)
